package com.example.service.impl;

import com.example.dao.EvaluateUserBehaviorMapper;
import com.example.microinvestcommon.pojo.Result;
import com.example.microinvestcommon.util.ThreadLocalUtil;
import com.example.service.OtherBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
public class OtherBusinessServiceImpl implements OtherBusinessService {


    private final EvaluateUserBehaviorMapper evaluateUserBehaviorMapper;

    @Autowired
    public OtherBusinessServiceImpl(EvaluateUserBehaviorMapper evaluateUserBehaviorMapper){
        this.evaluateUserBehaviorMapper=evaluateUserBehaviorMapper;
    }

    //用户收藏股票
    @Override
    @Transactional
    public Result<String> userStarStock(Long userId, String stockSymbol) {
        //判断用户是否收藏过此股票
        boolean isUserStarThisStock=evaluateUserBehaviorMapper.isUserStarThisStock(userId,stockSymbol);
        if(isUserStarThisStock){
            return Result.error("您已经收藏过此股票");
        }
        //收藏此股票
        evaluateUserBehaviorMapper.starThisStock(userId,stockSymbol);
        return Result.success("收藏成功");
    }

    @Override
    public Result userCancelStarThisStock(String stockSymbol) {
        Map<String,Long> map = ThreadLocalUtil.get();
        Long userId=map.get("userId");
        evaluateUserBehaviorMapper.cancelStarThisStock(userId,stockSymbol);
        return Result.success();
    }
}
